package com.neusoft.yiyang.diet.controller;

import com.neusoft.yiyang.common.entity.diet.MealTemplate;
import com.neusoft.yiyang.common.entity.diet.vto.DietPlanRequest;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItem;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItemDetail;
import com.neusoft.yiyang.diet.service.MealTemplateService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Tag(name = "膳食模板管理接口", description = "负责膳食模板的创建、查询及模板详情管理")
@RestController
@RequestMapping("/meal-templates")
public class MealTemplateController {

    @Autowired
    private MealTemplateService mealTemplateService;

    @Operation(summary = "添加膳食模板", description = "创建新的膳食模板（包含模板名称、适用场景等基础信息）")
    @PostMapping("/add")
    public void addMealTemplate(
            @Parameter(description = "膳食模板信息（含名称、描述、类别等）", required = true)
            @RequestBody MealTemplate mealTemplate) {
        mealTemplateService.addMealTemplate(mealTemplate);
    }

    @Operation(summary = "获取所有膳食模板", description = "查询系统中所有已创建的膳食模板列表")
    @GetMapping("/all")
    public List<MealTemplate> getAllMealTemplates() {
        return mealTemplateService.getAllMealTemplates();
    }

    @Operation(summary = "保存膳食计划模板", description = "将膳食计划（含每日各餐次食材）保存为模板")
    @PostMapping("saveTemplate")
    public void saveTemplate(
            @Parameter(description = "膳食计划请求数据（含模板名称、日期、餐次食材等）", required = true)
            @RequestBody DietPlanRequest dietPlanRequest){
        mealTemplateService.saveTemplate(dietPlanRequest);
    }

    @Operation(summary = "获取膳食模板内容", description = "根据日期和类别查询模板包含的餐次及对应食材信息")
    @GetMapping("getTemplate")
    public Map<String, Map<String, FoodItem>> getTemplate(
            @Parameter(description = "模板关联日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @Parameter(description = "模板类别（如早餐/午餐/特殊饮食）", required = true)
            @RequestParam("category") String category
    ){
        return mealTemplateService.getTemplate(date, category);
    }

    @Operation(summary = "获取膳食模板详情", description = "查询模板包含的餐次、食材及食材详细信息（如热量、禁忌等）")
    @GetMapping("getTemplateDetail")
    public Map<String, Map<String, FoodItemDetail>> getTemplateDetail(
            @Parameter(description = "模板关联日期（格式：yyyy-MM-dd）", required = true)
            @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
            @Parameter(description = "模板类别（如早餐/午餐/特殊饮食）", required = true)
            @RequestParam("category") String category
    ){
        return mealTemplateService.getTemplateDetail(date, category);
    }
}